package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.TruckAttributeEnum;
import cn.huiyunche.base.service.enums.TruckPicTypeEnum;
import cn.huiyunche.base.service.interfaces.CodeService;
import cn.huiyunche.base.service.mappers.BCodeMapper;
import cn.huiyunche.base.service.model.BCode;
import cn.huiyunche.base.service.model.BCodeExample;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LYN on 6/14/16.
 */
@Service
public class CodeServiceImpl implements CodeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CodeServiceImpl.class);

    @Autowired
    private BCodeMapper bCodeMapper = null;

    /**
     * 获取卡车属性选择项
     *
     * @return
     */
    @Override
    public List<Object> listTruckCode() {
        List<String> trucks = new ArrayList<String>();
        for (TruckAttributeEnum e : TruckAttributeEnum.values()) {
            if (!TruckAttributeEnum.TRUCK_HEIGHT.getValue().equals(e.getValue())
                    && !TruckAttributeEnum.TRUCK_LENGHT.getValue().equals(e.getValue())
                    && !TruckAttributeEnum.TRUCK_WIDTH.getValue().equals(e.getValue())) {
                trucks.add(e.getValue());
            }
        }
        List<Object> lists = new ArrayList<Object>();
        List<BCode> result = this.listCodes(trucks);
        LOGGER.info("list truck attributes: {}", JSON.toJSONString(result));
        for (TruckAttributeEnum e : TruckAttributeEnum.values()) {
            if (!TruckAttributeEnum.TRUCK_HEIGHT.getValue().equals(e.getValue())
                    && !TruckAttributeEnum.TRUCK_LENGHT.getValue().equals(e.getValue())
                    && !TruckAttributeEnum.TRUCK_WIDTH.getValue().equals(e.getValue())) {
                Map<String, Object> mapResult = new HashMap<String, Object>();
                List<Map<String, String>> list = getFromCode(result, e.getValue());
                mapResult.put("code", e.getValue());
                mapResult.put("name", TruckAttributeEnum.getByValue(e.getValue()).getText());
                mapResult.put("item", list);
                lists.add(mapResult);
            }
        }
        LOGGER.info("list truck attributes to map: {}", JSON.toJSONString(lists));
        return lists;
    }

    @Override
    public List<Object> listTruckSize() throws Exception {
        List<String> trucks = new ArrayList<String>();
        for (TruckAttributeEnum e : TruckAttributeEnum.values()) {
            if (TruckAttributeEnum.TRUCK_HEIGHT.getValue().equals(e.getValue())
                    || TruckAttributeEnum.TRUCK_LENGHT.getValue().equals(e.getValue())
                    || TruckAttributeEnum.TRUCK_WIDTH.getValue().equals(e.getValue())) {
                trucks.add(e.getValue());
            }
        }
        List<Object> lists = new ArrayList<Object>();
        List<BCode> result = this.listCodes(trucks);
        LOGGER.info("list truck attributes: {}", JSON.toJSONString(result));
        for (TruckAttributeEnum e : TruckAttributeEnum.values()) {
            if (TruckAttributeEnum.TRUCK_HEIGHT.getValue().equals(e.getValue())
                    || TruckAttributeEnum.TRUCK_LENGHT.getValue().equals(e.getValue())
                    || TruckAttributeEnum.TRUCK_WIDTH.getValue().equals(e.getValue())) {
                Map<String, Object> mapResult = new HashMap<String, Object>();
                List<Map<String, String>> list = getFromCode(result, e.getValue());
                mapResult.put("code", e.getValue());
                mapResult.put("name", TruckAttributeEnum.getByValue(e.getValue()).getText());
                mapResult.put("item", list);
                lists.add(mapResult);
            }
        }
        LOGGER.info("list truck attributes to map: {}", JSON.toJSONString(lists));
        return lists;
    }

    @Override
    public Map<String, List<Map<String, String>>> listPicAtrributeCode() {
        List<String> params = new ArrayList<String>();
        for (TruckPicTypeEnum e : TruckPicTypeEnum.values()) {
            params.add(e.getValue());
        }

        Map<String, List<Map<String, String>>> mapResult = new HashMap<String, List<Map<String, String>>>();

        List<BCode> result = this.listCodes(params);
        LOGGER.info("list pic attributes: {}", JSON.toJSONString(result));

        for (TruckPicTypeEnum e : TruckPicTypeEnum.values()) {
            List<Map<String, String>> list = getFromCode(result, e.getValue());
            mapResult.put(e.getValue().toLowerCase(), list);
        }
        LOGGER.info("list pic attributes to map: {}", JSON.toJSONString(mapResult));
        return mapResult;
    }

    private List<Map<String, String>> getFromCode(List<BCode> list, String classStr) {
        List<Map<String, String>> resList = new ArrayList<Map<String, String>>();
        for (int i = 0; i < list.size(); i++) {
            BCode code = list.get(i);
            String cStr = code.getClassCode();

            Map<String, String> codeObject = new HashMap<String, String>();
            codeObject.put("code", code.getCode());
            codeObject.put("name", code.getName());

            if (classStr.equals(cStr)) {
                resList.add(codeObject);
            }
        }
        return resList;
    }

    /**
     * 根据list 获取code
     *
     * @param params
     * @return
     */
    private List<BCode> listCodes(List<String> params) {
        BCodeExample codeSearch = new BCodeExample();
        codeSearch.createCriteria().andClassCodeIn(params);
        return this.getBCodeMapper().selectByExample(codeSearch);
    }

    @Override
    public BCode getByid(Long id) {
        return this.getBCodeMapper().selectByPrimaryKey(id);
    }

    public BCodeMapper getBCodeMapper() {
        return this.bCodeMapper;
    }

    @Override
    public List<BCode> listPicCode() throws Exception {
        List<Long> classId = new ArrayList<Long>();
        classId.add(9L);
        classId.add(10L);
        BCodeExample search = new BCodeExample();
        BCodeExample.Criteria criteria = search.createCriteria();
        criteria.andClassIdIn(classId);
        return this.getBCodeMapper().selectByExample(search);
    }

    @Override
    public List<BCode> getBCodeByClassCode(String classCode) {
        BCodeExample codeSearch = new BCodeExample();
        codeSearch.createCriteria().andClassCodeEqualTo(classCode);
        return this.getBCodeMapper().selectByExample(codeSearch);
    }
}
